package yl.hs.bmipfull.controllers.material;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.material.RawMaterialInfoEntity;
import yl.hs.bmipfull.service.material.IRawMaterialInfoService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.List;

/**
 * 作用: 原材信息控制层 <br/>
 * 创建时间: 2021-05-18 11:50 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "原材信息Web接口", tags = "采购管理——原材信息操作")
@RequestMapping(value = "/api/rawMaterial")
@RestController
public class RawMaterialInfoController extends ControllerBase {
    @Autowired
    IRawMaterialInfoService service;

    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "根据原材种类名称查询原材信息", notes = "根据原材种类名称查询原材信息")
    @RequestMapping(value = "/select/{cateSN}", method = RequestMethod.GET)
    public WebResult queryListByRawCate(@ApiParam(value = "原材种类名称") @PathVariable String cateSN) {
        try {
            List<RawMaterialInfoEntity> list = service.queryListByRawCateSn(cateSN);
            for (RawMaterialInfoEntity entity : list) {
                entity.setRmName(Tl.getRawMaterialFullName(entity));
            }
            return success("查询成功", list);
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据原材种类名称查询原材信息", notes = "根据原材种类名称查询原材信息")
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public WebResult queryListByRawCate() {
        try {
            List<RawMaterialInfoEntity> list = service.getAllInfo();
            for (RawMaterialInfoEntity entity : list) {
                entity.setRmName(Tl.getRawMaterialFullName(entity));
            }
            return success("查询成功", list);
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询原材信息", notes = "查询原材信息")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    @ResponseBody
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "分页查询原材信息", notes = "分页查询原材信息")
    @RequestMapping(value = "/info/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery, @PathVariable int rowNum, @PathVariable int page) {
        try {
            svSVC.updateUserMenuView(pageQuery);
            return success("查询成功", service.queryList(pageQuery, rowNum, page));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "保存原材信息", notes = "查询/更新原材信息信息")
    @RequestMapping(value = "/info", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveRawMaterialInfoEntity(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "根据sn查询原材信息", notes = "根据sn查询原材信息")
    @RequestMapping(value = "/infoBySN/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }

    @ApiOperation(value = "根据sn查询原材信息", notes = "根据sn查询原材信息")
    @RequestMapping(value = "/info/{cateSN}", method = RequestMethod.GET)
    public WebResult findByRawCateSn(@PathVariable String cateSN) {
        return success("查询成功", service.findByRawCateSn(cateSN));
    }

    @ApiOperation(value = "根据sn删除原材信息", notes = "根据sn删除原材信息")
    @RequestMapping(value = "/info/{sn}", method = RequestMethod.DELETE)
    public WebResult DeleteBySn(@PathVariable String sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");

        }
    }
    @ApiOperation(value = "根据编号修改状态", notes = "根据编号修改状态")
    @RequestMapping(value = "/info/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = service.changeStateBySn(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
}
